﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Angel.Daos.IManager;
using Angel.Entities;
namespace Angel.Daos.Manager
{
    public class GroupManager: IGroupManager
    {
        AngelDataContext ctx = new AngelDataContext();

        public IEnumerable<Group> GetAll()
        {
            return ctx.Groups.ToList();
        }
        public Group GetByID(int id)
        {
            return ctx.Groups.SingleOrDefault(g => g.GroupID == id);
        }
        public void Add(Group group)
        {     
            ctx.Groups.InsertOnSubmit(group);
            ctx.SubmitChanges();
        }

        public void Delete(Group group)
        {
            ctx.Group_Users.DeleteAllOnSubmit(group.Group_Users);
            ctx.Groups.DeleteOnSubmit(group);
            ctx.SubmitChanges();
        }

        public void Delete(int id)
        {
            throw new NotImplementedException();
        }

        /// Author: Phan Duc Chien
        public IEnumerable<Group> Search(String groupName)
        {
            //Return list of groups to be searched.
            IEnumerable<Group> listGroup = (from row in ctx.Groups
                                            where row.GroupName.ToString().Contains(groupName)
                                            select row).OrderBy(m => m.GroupName);
            return listGroup;
        }

        public IEnumerable<Group_User> GetData()
        {
            var result = from item in ctx.Group_Users
                         orderby item.Group.GroupName
                         select item;
            return result;
        }
    }
}
